package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.OldInConstant;
import com.csun.cmny.provider.model.domain.OmcChannel;
import com.csun.cmny.provider.model.domain.OmcSubscribe;
import com.csun.cmny.provider.model.dto.SubscribeQueryDto;
import com.csun.cmny.provider.model.dto.UnSubscribeDto;
import com.csun.cmny.provider.model.dto.subscribe.subscribeSaveDto;
import com.csun.cmny.provider.model.dto.subscribe.subscribeUpdateDto;
import com.csun.cmny.provider.model.dto.subscribe.unsubscribeDto;
import com.csun.cmny.provider.model.vo.RpcRoleVo;
import com.csun.cmny.provider.model.vo.subscribe.SubscribeVo;
import com.csun.cmny.provider.pojo.Bed;
import com.csun.cmny.provider.pojo.Room;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.BedService;
import com.csun.cmny.provider.service.OmcSubscribeService;
import com.csun.cmny.provider.service.RoomService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.provider.utils.PhoneUtil;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;


@RestController
@RequestMapping(value = "/subscribe", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcSubscribeController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcSubscribeService omcSubscribeService;
    @Resource
    private BedService bedService;
    @Resource
    private RoomService roomService;
    @Resource
    private UacRpcService uacRpcService;


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增预约")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addSubscribe(@RequestBody @Valid subscribeSaveDto subscribeSaveDto, BindingResult br) {
        Room room=null;

        //校验此手机号的用户是否已经预约
        OmcSubscribe omcSubscribe1 = new OmcSubscribe();
        omcSubscribe1.setOldPhone(subscribeSaveDto.getOldPhone());
        omcSubscribe1.setState(0);
        List<OmcSubscribe> select = omcSubscribeService.select(omcSubscribe1);
        if(PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("subscribe.phone.repeat", null));
        }

        if(PublicUtil.isEmpty(subscribeSaveDto.getFloorNo())){

            Bed bedVo = bedService.selectById(subscribeSaveDto.getBedId().intValue());
            if(PublicUtil.isNotEmpty(bedVo)){
                room = roomService.selectById(bedVo.getRoomId());
            }

        }



        //新增预约数据
        OmcSubscribe omcSubscribe = new OmcSubscribe();
        BeanUtils.copyProperties(subscribeSaveDto,omcSubscribe);
        omcSubscribe.setOperator(getLoginAuthDto().getUserName());
        omcSubscribe.setGroupId(getLoginAuthDto().getGroupId());
        omcSubscribe.setState(0);
        omcSubscribe.setUserId(getLoginAuthDto().getUserId());
        if(PublicUtil.isNotEmpty(room)){
            omcSubscribe.setFloorNo(room.getFloorNo());
        }

        Integer relust=omcSubscribeService.insertSubscribe(omcSubscribe);

        Bed bed = new Bed();
        bed.setBedId(subscribeSaveDto.getBedId().intValue());
        bed.setStatus(3);
        bedService.updateById(bed);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),relust);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "退订预约")
    @RequestMapping(value = "/unsubscribe", method = RequestMethod.POST)
    public Wrapper unsubscribe(@RequestBody @Valid unsubscribeDto unsubscribeDto){

        OmcSubscribe omcSubscribe = omcSubscribeService.selectByKey(unsubscribeDto.getId());
        if(PublicUtil.isEmpty(omcSubscribe)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("subscribe.null", null));
        }
        if(omcSubscribe.getState()==1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("subscribe.un.error", null));
        }

        Bed bed = new Bed();
        bed.setBedId(omcSubscribe.getBedId().intValue());
        bed.setStatus(0);
        bedService.updateById(bed);


        int relust = omcSubscribeService.updateState(unsubscribeDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),relust);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询预约")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper select(@RequestBody @Valid SubscribeQueryDto subscribeQueryDto) {


        // 获取当前用户角色
        List<RpcRoleVo> roleVoList = uacRpcService.queryRoleListByUserId(getLoginAuthDto().getUserId());

        //如果角色是销售员,那么只能看自己录入的信息
        for (RpcRoleVo rpcRoleVo : roleVoList) {
            String roleCode = rpcRoleVo.getRoleCode();
            if(roleCode.equals(OldInConstant.SALESMAN)){
                subscribeQueryDto.setUserId(getLoginAuthDto().getUserId());
            }
        }


        subscribeQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        List<SubscribeVo>omcSubscribes=omcSubscribeService.selectSubscribe(subscribeQueryDto);



            for (SubscribeVo subscribeVo : omcSubscribes) {
                if(!subscribeVo.getUserId().equals(getLoginAuthDto().getUserId())){
                    subscribeVo.setIsMy(1);
                }
            }


        PageInfo<SubscribeVo> pageInfo = new PageInfo<>(omcSubscribes);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改预约")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper update(@RequestBody @Valid  subscribeUpdateDto subscribeUpdateDto, BindingResult br) {

        if(PublicUtil.isNotEmpty(subscribeUpdateDto.getOldPhone())){
            OmcSubscribe omcSubscribe = new OmcSubscribe();
            omcSubscribe.setOldPhone(subscribeUpdateDto.getOldPhone());
            omcSubscribe.setState(0);
            List<OmcSubscribe> select = omcSubscribeService.select(omcSubscribe);
            if(PublicUtil.isNotEmpty(select)&&!select.get(0).getId().equals(subscribeUpdateDto.getId())){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("subscribe.phone.repeat", null));
            }

        }
        //将原床位更改为空床
        OmcSubscribe omcSubscribe = omcSubscribeService.selectByKey(subscribeUpdateDto.getId());
        if(!omcSubscribe.getBedId().equals(subscribeUpdateDto.getBedId())){

            //将原床位更改为空床
            Bed bed = new Bed();
            bed.setBedId(omcSubscribe.getBedId().intValue());
            bed.setStatus(0);
            int update = bedService.updateById(bed);
            if(update>0){
                //将新床位更改为预约
                Bed bed1 = new Bed();
                bed1.setBedId(subscribeUpdateDto.getBedId().intValue());
                bed1.setStatus(3);
                bedService.updateById(bed1);
            }

        }

        OmcSubscribe subscribe = new OmcSubscribe();
        BeanUtils.copyProperties(subscribeUpdateDto,subscribe);
        Integer relust=omcSubscribeService.update(subscribe);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),relust);
    }


}
